function out = CampbellSchillerRegression(Data,shortRate,termPrem,fwdPrem,maturities,h)

% IMPORTANT: we have monthly data but we do not necessarily have all
% monthly maturities in the cross-section dimension, i.e. yields of
% maturities 0.1, 0.2, 0.3 months ets. 
% We can easily correct for this by ensuring that one time step in the
% time series dimension also corresponds to one time step in the
% cross-section dimension.
% Suppose we have monthly yields in the time series dimension, but we only 
% observe yields every 6th months in the cross-section dimension. Then, 
% we let h = 6;

T              = size(Data,1);
n              = length(maturities);
CSBetta        = NaN(2,n);
CSBetta_tstat  = NaN(2,n);
for i=2:n
%    Y = Data(1+h:T,i)-Data(1:T-h,i+1)...
%        - termPrem(1+h:T,i)...
%        + termPrem(1:T-h,i)...
%        + h/(maturities(1,i+1)-h)*fwdPrem(1:T-h,i);
    Y = Data(1+h:T,i-1)-Data(1:T-h,i)...
        - termPrem(1+h:T,i-1)...
        + termPrem(1:T-h,i-1)...
        + h/(maturities(1,i)-h)*fwdPrem(1:T-h,i-1);
   x               = h/(maturities(1,i)-h)*(Data(1:T-h,i)-shortRate(1:T-h,1));
   X               = [ones(T-h,1) x];
   res             = nwest(Y,X,2*h);
   CSBetta(:,i)    = res.beta;
   CSBetta_tstat(:,i) = res.tstat;
   
end
out.CSBetta        = CSBetta;
out.CSBetta_tstat  = CSBetta_tstat;
out.maturities     = maturities;
out.h              = h;